package cn.zcf.org.controller;

import cn.zcf.basic.exception.BusinessException;
import cn.zcf.basic.utils.ExcelUtils;
import cn.zcf.basic.utils.JsonResult;
import cn.zcf.basic.utils.PageList;
import cn.zcf.org.domain.Shop;
import cn.zcf.org.domain.ShopAuditLog;
import cn.zcf.org.dto.ShopDto;
import cn.zcf.org.query.ShopQuery;
import cn.zcf.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "超市的API",description="超市相关的CRUD功能")
public class ShopController {
    @Autowired
    private IShopService service;
    @GetMapping
    @ApiOperation(value = "查询全部" )
    public List<Shop> loadAll(){
        return service.loadAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Shop loadById(@PathVariable("id") Long id){
        return service.loadById(id);
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }
    @PutMapping
    @ApiOperation(value = "新增或修改" )
    public JsonResult addOrUpt(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                service.add(shop);
            }
            else {
                service.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Shop> pageQuery(@RequestBody ShopQuery query){
        try {
            return service.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Shop>();
        }

    }
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            service.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }
    @ApiOperation(value = "商家入驻",notes = "")
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            service.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException e){ //业务异常捕获
            return JsonResult.me().setMsg("入驻失败!"+e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }
    /**
     * 接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
//前端传递的是数组 - 后端既可以用数组,也可以用集合
    public JsonResult pass(@RequestBody ShopAuditLog log){
        try {
            service.pass(log);
            return new JsonResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }


    /**
     * 接口：店铺审核驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
//前端传递的是数组 - 后端既可以用数组,也可以用集合
    public JsonResult reject(@RequestBody ShopAuditLog log){
        try {
            service.reject(log);
            return new JsonResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }
    /**
     * 导出
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            // 查询所有shop数据
            List<Shop> list = service.loadAll();
            // d调用工具了  (List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response)
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            // 导入  ，调用工具类  MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass

            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            // 保存到数据库表中
            list.forEach(System.out::println);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return service.getEcharts();
    }
}
